// pages/giftShop/afterSale/address/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: null, //地址详情
    type: null, //1编辑取件地址 2编辑收货地址
    cityShow: false,
    provincesShow: false,
    provinceName: '', //省
    provinceCode: '', //省
    cityName: '', //市
    cityCode: '', //市
    districtName: '', //区
    districtCode: '', //区
    areaCode:'',//区
    areaName:'',//区
    streetName: '', //街道
    streetCode: '', //街道
    receiver: '', //收货人姓名
    receiverPhone: '', //收货人电话
    receiverAddress: '', //详细地址
    disabled: true, //按钮是否能点击
    addressData: {}, //省市区选择传参
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const address = JSON.parse(decodeURIComponent(options.address)) || null
    this.setData({
      type:parseInt(options.type) || null, //1编辑取件地址 2编辑收货地址
      address: address || null, //地址信息
      areaCode: address.areaCode || '',
      areaName: address.areaName || '',
      cityCode: address.cityCode || '',
      cityName: address.cityName || '',
      provinceCode: address.provinceCode || '',
      provinceName: address.provinceName || '',
      receiver: address.receiver || '',
      receiverAddress: address.receiverAddress || '',
      receiverPhone: address.receiverPhone || '',
      streetCode: address.streetCode || '',
      streetName: address.streetName || '',
    })
    wx.setNavigationBarTitle({
      title: options.type == 1 ? '编辑取件地址' : '编辑收货地址'
    })
    this.checkInfo();
  },

  /**
   * 选择城市
   */
  getCity: function (e) {
    this.setData({
      provinceName: e.detail.provinceName, //省
      provinceCode: e.detail.provinceCode, //省
      cityName: e.detail.cityName, //市
      cityCode: e.detail.cityCode, //市
      areaName: e.detail.districtName, //区
      areaCode: e.detail.districtCode, //区
      streetName: e.detail.streetName, //街道
      streetCode: e.detail.streetCode //街道
    })
  },

  /**
   * 选择所在地区
   */
  selectCity: function () {
    let addressInfo={
      provinceCode: this.data.provinceCode, //省编码
      provinceName: this.data.provinceName, //省名称
      cityCode: this.data.cityCode, //市编码
      cityName: this.data.cityName, //市名称
      districtCode: this.data.areaCode, //区编码
      districtName: this.data.areaName, //区名称
      streetCode: this.data.streetCode, //街道编码
      streetName: this.data.streetName, //街道名称
      cityShow: true
    }
    this.setData({
      addressData:addressInfo
    })
  },
  /**
   * 收货人
   */
  setReceiverName(e) {
    var name = /[^A-Za-z0-9\u4e00-\u9fa5]/g;
    var value = e.detail.value;
    if (name.test(value)) {
      this.setData({
        receiver: this.data.receiverName
      })
    } else {
      this.setData({
        receiver: value
      })
    }
    this.checkInfo();
  },

  /**
   * 手机号
   */
  setReceiverPhone(e) {
    var value = e.detail.value;
    this.setData({
      receiverPhone: value
    })
    this.checkInfo();
  },

  /**
   * 详细地址
   */
  setFullAddress(e) {
    var value = e.detail.value;
    this.setData({
      receiverAddress: value
    })
    this.checkInfo();
  },

  /**
   * 验证输入的内容
   */
  checkInfo: function () {
    var that = this;
    var res = /^1\d{10}$/;
    var name = /[^A-Za-z0-9\u4e00-\u9fa5]/g;
    if (that.data.receiver != '' && !name.test(that.data.receiver) && that.data.receiverPhone != "" && that.data.receiverPhone.length == 11 && res.test(that.data.receiverPhone) && that.data.provinceCode != '' && that.data.cityCode != '' && that.data.receiverAddress != '') {
      that.setData({
        disabled: false,
      })
    } else {
      that.setData({
        disabled: true,
      })
    }
  },

  /**
   * 保存
   */
  saveAddress: function () {
    wx.reportAnalytics('gift_service_address');
    let addressInfo = {
      areaCode: this.data.areaCode,
      areaName: this.data.areaName,
      cityCode: this.data.cityCode,
      cityName: this.data.cityName,
      provinceCode: this.data.provinceCode,
      provinceName: this.data.provinceName,
      receiver: this.data.receiver,
      receiverAddress: this.data.receiverAddress,
      receiverPhone: this.data.receiverPhone,
      streetCode: this.data.streetCode,
      streetName: this.data.streetName,
    }
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    if(this.data.type==1){ //1编辑取件地址 2编辑收货地址
      prevPage.setData({
        pickware: addressInfo
      })
    }else if(this.data.type==2){
      prevPage.setData({
        returnware: addressInfo
      })
    }
    wx.navigateBack({
      delta: 1
    })
  },
})